package com.onLineCar.job.task;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.common.core.utils.StringUtils;
import com.onLineCar.job.feign.OpeignFeign;
import com.onLineCar.vehicle.entity.TblOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author onLineCar
 */
@Component("myTask")
public class MyTask {
    @Autowired
    private OpeignFeign opeignFeign;

    @Autowired
    private RedisTemplate<String , String> redisTemplate;
    public void ryMultipleParams (String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams (String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams () {
        System.out.println("执行无参方法");
    }

    /**
     * @description:30秒扫描一次订单表将数据存入redis
     * @author: Lxk
     * @date: 2023/12/19 18:59
     * @param: []
     * @return: void
     **/
    public void onLineCarOrder(){
        Result<List<TblOrder>> listResult = opeignFeign.selectAllCloseOrder();
        List<TblOrder> listResultData = listResult.getData();

        redisTemplate.delete("close_orders");

        if(listResultData!=null){
             //将每个对象转换为 JSON 字符串并存储到 Redis 中
            System.out.println("==将历史订单数据添加进redis===");
            for (TblOrder order : listResultData) {
                try {
                    ObjectMapper objectMapper = new ObjectMapper();
                    String json = objectMapper.writeValueAsString(order);
                    redisTemplate.opsForList().rightPush("close_orders", json);
                } catch (JsonProcessingException e) {
                    // 处理异常情况
                    e.printStackTrace();
                }
            }
        }
    }
}
